package com.tangkun.environment.fragment.home;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tangkun.environment.R;
import com.tangkun.environment.activity.BianJiZiLiaoActivity;
import com.tangkun.environment.activity.ErWeiMaActivity;
import com.tangkun.environment.activity.HuanJingZhaoPianActivity;
import com.tangkun.environment.activity.ImageActivity;
import com.tangkun.environment.activity.LocationDemo4;
import com.tangkun.environment.application.Session;
import com.tangkun.environment.entity.shouye.EnterpriseViewReturn;
import com.tangkun.environment.fragment.BaseFragment;
import com.tangkun.environment.network.GlobalUrl;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxFragmentHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.utils.LogUtils;
import com.tangkun.environment.view.CircleImageView;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * 基础数据Fragment
 */

public class JiChuShuJuFragment extends BaseFragment {

    @BindView(R.id.tv_jichushuju_tips)
    TextView tvJichushujuTips;
    @BindView(R.id.iv_jichushuju_tips)
    ImageView ivJichushujuTips;
    @BindView(R.id.ll_jichushuju_tips)
    LinearLayout llJichushujuTips;
    @BindView(R.id.civ_jichushuju_photo)
    CircleImageView civJichushujuPhoto;
    @BindView(R.id.iv_jichushuju_yichang)
    ImageView ivJichushujuYichang;
    @BindView(R.id.tv_jichushuju_name)
    TextView tvJichushujuName;
    @BindView(R.id.tv_jichushuju_dingwei)
    TextView tvJichushujuDingwei;
    @BindView(R.id.tv_jichushuju_erweima)
    ImageView tvJichushujuErweima;
    @BindView(R.id.tv_jichushuju_dianhua)
    TextView tvJichushujuDianhua;
    @BindView(R.id.ll_jichushuju_dianhua)
    LinearLayout llJichushujuDianhua;
    @BindView(R.id.tv_jichushuju_dizhi)
    TextView tvJichushujuDizhi;
    @BindView(R.id.ll_jichushuju_dizhi)
    LinearLayout llJichushujuDizhi;
    @BindView(R.id.tv_jichushuju_huanjingzhaopian)
    TextView tvJichushujuHuanjingzhaopian;
    @BindView(R.id.tv_jichushuju_huanjingzhaopian_num)
    TextView tvJichushujuHuanjingzhaopianNum;
    @BindView(R.id.ll_jichushuju_huanjingzhaopian)
    LinearLayout llJichushujuHuanjingzhaopian;
    @BindView(R.id.tv_jichushuju_chanyexiehui)
    TextView tvJichushujuChanyexiehui;
    @BindView(R.id.ll_jichushuju_chanyexiehui)
    LinearLayout llJichushujuChanyexiehui;
    @BindView(R.id.tv_jichushuju_yingyehao)
    TextView tvJichushujuYingyehao;
    @BindView(R.id.ll_jichushuju_yingyehao)
    LinearLayout llJichushujuYingyehao;
    @BindView(R.id.tv_jichushuju_yingyezhizhao)
    TextView tvJichushujuYingyezhizhao;
    @BindView(R.id.iv_jichushuju_yingyezhizhao)
    ImageView ivJichushujuYingyezhizhao;
    @BindView(R.id.ll_jichushuju_yingyezhizhao)
    LinearLayout llJichushujuYingyezhizhao;
    @BindView(R.id.ll_jichushuju_xiugaiziliao)
    LinearLayout llJichushujuXiugaiziliao;
    @BindView(R.id.tv_jichushuju_xiugaiziliao_tips)
    TextView tvJichushujuXiugaiziliaoTips;
    @BindView(R.id.tv_jichushuju_xiugaiziliao)
    TextView tvJichushujuXiugaiziliao;
    @BindView(R.id.divider_jichushuju_chanyexiehui)
    View dividerJichushujuChanyexiehui;
    @BindView(R.id.divider_jichushuju_yingyehao)
    View dividerJichushujuYingyehao;
    @BindView(R.id.divider_jichushuju_yingyezhizhao)
    View dividerJichushujuYingyezhizhao;
    /**
     * 企业ID
     */
    private String id = "";
    /**
     * 是否是我的企业
     */
    private boolean isMine = false;
    /**
     * 环境照片图片集合
     */
    private List<String> urlList = new ArrayList<>();
    /**
     * 营业执照图片集合
     */
    private List<String> urlList2 = new ArrayList<String>();
    /**
     * 经纬度
     */
    private String lat = "";
    /**
     * 经纬度
     */
    private String lng = "";
    /**
     * 地址
     */
    private String address = "";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_jichushuju, container, false);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        id = getArguments().getString("id");
        initView();
        initData();
        enterpriseView();
        return view;
    }

    private void initView() {
        if (isMine) {
            llJichushujuXiugaiziliao.setVisibility(View.VISIBLE);
        } else {
            llJichushujuXiugaiziliao.setVisibility(View.GONE);
        }
    }

    private void initData() {

    }

    /**
     * 企业中心
     */
    private void enterpriseView() {
        fromNetwork.enterpriseView(id).compose(new RxFragmentHelper<EnterpriseViewReturn>().ioMain(getActivity(), JiChuShuJuFragment.this, true))
                .subscribe(new MySubscribe<EnterpriseViewReturn>(getActivity(), true) {
                    @Override
                    protected void success(EnterpriseViewReturn resultBean) {
                        if (resultBean.data != null) {
                            //赋值经纬度地址，用于跳转定位页面
                            lat = resultBean.data.lat;
                            lng = resultBean.data.lng;
                            address = resultBean.data.address;

                            //顶部小黄条
                            if (resultBean.data.errornum > 0) {
                                tvJichushujuTips.setText("检测企业" + resultBean.data.errornum + "组基础数据异常，请修改、补充、完善。");
                                llJichushujuTips.setVisibility(View.VISIBLE);
                            } else {
                                llJichushujuTips.setVisibility(View.GONE);
                            }
                            //头像
                            Glide.with(getActivity())
                                    .load(GlobalUrl.IMG_URL + resultBean.data.img)
                                    .bitmapTransform(new CropCircleTransformation(getActivity()))
                                    .placeholder(R.mipmap.ic_guanlianqiye_shangchuandianzhao)
                                    .dontAnimate()
                                    .into(civJichushujuPhoto);
                            //店铺名字
                            if (!TextUtils.isEmpty(resultBean.data.name)) {
                                tvJichushujuName.setText(resultBean.data.name);
                            } else {
                                tvJichushujuName.setText("");
                            }
                            //定位
                            if (!TextUtils.isEmpty(resultBean.data.address)) {
                                tvJichushujuDingwei.setText(resultBean.data.address);
                            } else {
                                tvJichushujuDingwei.setText("");
                            }
                            //电话
                            if (!TextUtils.isEmpty(resultBean.data.tel)) {
                                tvJichushujuDianhua.setText(resultBean.data.tel);
                            } else {
                                tvJichushujuDianhua.setText("");
                            }
                            //地址
                            if (!TextUtils.isEmpty(resultBean.data.address)) {
                                tvJichushujuDizhi.setText(resultBean.data.address);
                            } else {
                                tvJichushujuDizhi.setText("");
                            }
                            //环境图片
                            if (!TextUtils.isEmpty(resultBean.data.environimgs)) {
                                String[] environimgs = resultBean.data.environimgs.split(",");
                                urlList = Arrays.asList(environimgs);//赋值环境照片集合
                                tvJichushujuHuanjingzhaopianNum.setText(environimgs.length + "");
                            } else {
                                tvJichushujuHuanjingzhaopianNum.setText("0");
                            }
                            //产业协会
                            if (!TextUtils.isEmpty(resultBean.data.legalname)) {
                                tvJichushujuChanyexiehui.setText(resultBean.data.legalname);
                            } else {
                                tvJichushujuChanyexiehui.setText("");
                            }
                            //营业号
                            if (!TextUtils.isEmpty(resultBean.data.license)) {
                                tvJichushujuYingyehao.setText(resultBean.data.license);
                            } else {
                                tvJichushujuYingyehao.setText("");
                            }
                            //营业执照
                            if (!TextUtils.isEmpty(resultBean.data.licenseimg1)) {
                                urlList2.clear();
                                urlList2.addAll(Arrays.asList(resultBean.data.licenseimg1.split(",")));
                                Glide.with(getActivity())
                                        .load(GlobalUrl.IMG_URL + urlList2.get(0))
                                        .placeholder(R.mipmap.ic_place_holder)
                                        .dontAnimate()
                                        .into(ivJichushujuYingyezhizhao);
                                ivJichushujuYingyezhizhao.setVisibility(View.VISIBLE);
                            } else {
                                ivJichushujuYingyezhizhao.setVisibility(View.GONE);
                            }
                            //是不是我的企业
                            if (resultBean.data.owner == 1) {
                                //是我的企业 企业用户或者协会巡查员都可以修改
                                if (Session.getInstance().getUserRole().equals("企业用户") || Session.getInstance().getUserRole().equals("协会巡查员")) {
                                    llJichushujuXiugaiziliao.setVisibility(View.VISIBLE);
                                } else {
                                    llJichushujuXiugaiziliao.setVisibility(View.GONE);
                                }

                                //后三项 可见
                                llJichushujuChanyexiehui.setVisibility(View.VISIBLE);
                                llJichushujuYingyehao.setVisibility(View.VISIBLE);
                                llJichushujuYingyezhizhao.setVisibility(View.VISIBLE);
                                dividerJichushujuChanyexiehui.setVisibility(View.VISIBLE);
                                dividerJichushujuYingyehao.setVisibility(View.VISIBLE);
                                dividerJichushujuYingyezhizhao.setVisibility(View.VISIBLE);
                            } else {
                                //不是我的企业
                                llJichushujuXiugaiziliao.setVisibility(View.GONE);

                                //后三项 不可见
                                llJichushujuChanyexiehui.setVisibility(View.GONE);
                                llJichushujuYingyehao.setVisibility(View.GONE);
                                llJichushujuYingyezhizhao.setVisibility(View.GONE);
                                dividerJichushujuChanyexiehui.setVisibility(View.GONE);
                                dividerJichushujuYingyehao.setVisibility(View.GONE);
                                dividerJichushujuYingyezhizhao.setVisibility(View.GONE);
                            }
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage + "");
                    }
                });
    }

    @OnClick({R.id.iv_jichushuju_tips, R.id.tv_jichushuju_erweima, R.id.ll_jichushuju_dianhua, R.id.ll_jichushuju_dizhi, R.id.ll_jichushuju_huanjingzhaopian, R.id.ll_jichushuju_chanyexiehui, R.id.ll_jichushuju_yingyehao, R.id.ll_jichushuju_yingyezhizhao, R.id.tv_jichushuju_xiugaiziliao})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_jichushuju_tips://隐藏顶部小黄条
                llJichushujuTips.setVisibility(View.GONE);
                break;
            case R.id.tv_jichushuju_erweima:
                Intent erweimaIntent = new Intent(getActivity(), ErWeiMaActivity.class);
                erweimaIntent.putExtra("id", id);
                JumpHelper.jump(getActivity(), erweimaIntent, false);
                break;
            case R.id.ll_jichushuju_dianhua:
                call();
                break;
            case R.id.ll_jichushuju_dizhi://地址
                if (!TextUtils.isEmpty(lng) && !TextUtils.isEmpty(lat)) {
                    Intent dizhiIntent = new Intent(getActivity(), LocationDemo4.class);
                    dizhiIntent.putExtra("lat", lat);
                    dizhiIntent.putExtra("lng", lng);
                    dizhiIntent.putExtra("address", address);
                    JumpHelper.jump(getActivity(), dizhiIntent, false);
                } else {
//                    showToast("经纬度为空");
                    LogUtils.e("经纬度为空");
                }
                break;
            case R.id.ll_jichushuju_huanjingzhaopian://环境照片
                Intent huanjingzhaopianIntent = new Intent(getActivity(), HuanJingZhaoPianActivity.class);
                huanjingzhaopianIntent.putExtra("urlList", (Serializable) urlList);
                huanjingzhaopianIntent.putExtra("id", id);
                JumpHelper.jump(getActivity(), huanjingzhaopianIntent, false);
                break;
            case R.id.ll_jichushuju_chanyexiehui://产业协会
                break;
            case R.id.ll_jichushuju_yingyehao://营业号
                break;
            case R.id.ll_jichushuju_yingyezhizhao://营业执照
                if (urlList2 != null && urlList2.size() > 0) {
                    Intent intent = new Intent(getActivity(), ImageActivity.class);
                    intent.putExtra("urlList", (Serializable) urlList2);//图片地址集合
                    intent.putExtra("index", 0);//索引
                    JumpHelper.jump(getActivity(), intent, false);
                }
                break;
            case R.id.tv_jichushuju_xiugaiziliao://修改资料
                Intent modifyIntent = new Intent(getActivity(), BianJiZiLiaoActivity.class);
                modifyIntent.putExtra("id", id);//id
                JumpHelper.jump(getActivity(), modifyIntent, false);
                break;
        }
    }

    /**
     * 打电话
     */
    private void call() {
        String tel = tvJichushujuDianhua.getText().toString();
        if (!TextUtils.isEmpty(tel)) {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            Uri data = Uri.parse("tel:" + tel);
            intent.setData(data);
            startActivity(intent);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //修改企业资料成功刷新
    @Subscriber(tag = "QiYeZhongXinActivity.refresh")
    private void refresh(boolean b) {
        enterpriseView();
    }
}
